// pages/myOrder/myOrder.js

const {
    goodsAll
} = require("../../services/myOrderReq");
const {
    CourseAllMyOrder
} = require("../../services/myOrderReq");
const {
    myActivityAllOrder
} = require("../../services/myOrderReq");
const {
    goodsAllOrderByIdStatus
} = require('../../services/myOrderReq');

Page({

    data: {
        orderList: [],
        myUserId: null,
        CourseOrderList: [],
        ActivityOrderList: [],
        type: 0,
        goodsOrderObj: {
            pageNum: 1,
            pageSize: 10,
            isFinish: false,
            status: 1,
        },
        courseOrderObj: {
            pageNum: 1,
            pageSize: 10,
            isFinish: false,
        },
        activityOrderObj: {
            pageNum: 1,
            pageSize: 10,
            isFinish: false,
        },
    },
    onReachBottom() {
        const {
            type,
            goodsOrderObj,
            courseOrderObj,
            activityOrderObj,
        } = this.data;
        switch (type) {
            case 0:
                goodsOrderObj["pageNum"] = goodsOrderObj["pageNum"] + 1;
                this.setData({
                    goodsOrderObj
                });
            case 1:
                courseOrderObj["pageNum"] = courseOrderObj["pageNum"] + 1;
                this.setData({
                    courseOrderObj
                });
            case 2:
                activityOrderObj["pageNum"] = activityOrderObj["pageNum"] + 1;
                this.setData({
                    activityOrderObj
                });
        }
        this.getDiff(type)
    },
    // /**
    //  * 生命周期函数--监听页面加载
    //  */
    handleChangeType(e) {
        const {
            index
        } = e.detail;
        this.getDiff(index);
    },
    handleChangeStatus(e) {
        const {
            index
        } = e.detail;
        this.setData({
            goodsOrderObj: {
                pageNum: 1,
                pageSize: 10,
                isFinish: false,
                status: index + 1,
            }
        });
        return this.handleGetOrders(0, index);

    },
    getDiff(index) {
        return this.handleGetOrders(index);
    },
    onShow() {
        this.handleGetOrders(0);
    },
    handleGetOrders(type) {
        if (type === 0) {
            return this.handleGetGoodsOrder()
        }
        if (type === 1) {
            return this.handleGetCourseOrder();
        }
        return this.handleGetActivityOrder();
    },

    handleGetGoodsOrder() {
        const {
            orderList,
            goodsOrderObj
        } = this.data;
        if (goodsOrderObj.isFinish) {
            return;

            //     return wx.showToast({
            //     title: "到底了",
            //     icon: "error",
            // });
        }
        wx.showLoading({
            title: "加载中……",
            icon: "error"
        })
        goodsAll(goodsOrderObj, res => {
            const {
                list,
                totalCount
            } = res.data.data;
            console.log(res.data.data);
            wx.hideLoading();
            goodsOrderObj["isFinish"] = orderList.length >= totalCount;
            this.setData({
                orderList: list,
                goodsOrderObj
            })

        })
    },
    handleGetCourseOrder() {
        const {
            CourseOrderList,
            courseOrderObj
        } = this.data;
        if (courseOrderObj.isFinish) {
            return;
            // return wx.showToast({
            //     title: "到底了",
            //     icon: "error",
            // });
        }
        wx.showLoading({
            title: "加载中……",
            icon: "error"
        })
        CourseAllMyOrder(courseOrderObj, res => {
            const {
                list,
                totalCount
            } = res.data.data;
            wx.hideLoading();
            courseOrderObj["isFinish"] = CourseOrderList.length >= totalCount;
            if (!CourseOrderList.length >= totalCount) {
                this.setData({
                    CourseOrderList: CourseOrderList.concat(list),
                    courseOrderObj
                })
            }

        })
    },
    handleGetActivityOrder() {
        const {
            ActivityOrderList,
            activityOrderObj
        } = this.data;
        if (activityOrderObj.isFinish) {
            // return wx.showToast({
            //     title: "到底了",
            //     icon: "error",
            // });
            return;
        }
        wx.showLoading({
            title: "加载中……",
            icon: "error"
        })
        myActivityAllOrder(activityOrderObj, res => {
            const {
                list,
                totalCount
            } = res.data.data;
            console.log(`totalCount-->`, totalCount)
            console.log(`ActivityOrderList-->`, ActivityOrderList);
            wx.hideLoading();
            activityOrderObj.isFinish = ActivityOrderList.length >= totalCount;
            if (!ActivityOrderList.length >= totalCount) {
                this.setData({
                    ActivityOrderList: ActivityOrderList.concat(list),
                    activityOrderObj
                })
            }

        })
    },


})